package com.mty.springboot.controller;

import com.mty.springboot.model.Admin;
import com.mty.springboot.model.User;
import com.mty.springboot.model.PageInfo;
import com.mty.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @description: 控制层
 * @author: mty
 */
@Controller
public class UserController {

    // 依赖注入
    @Autowired
    private UserService userService;

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findUser")
    public String findUser(Integer pageIndex, Integer pageSize,String idcard,String realname, Model model,HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        mp.put("idcard",idcard);
        mp.put("realname",realname);
        PageInfo<User> pageList = userService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "UserList";
    }

    /**
     * 添加用户（审核通过用户账号注册）
     */
    @RequestMapping(value = "/addUser" ,method = RequestMethod.POST)
    @ResponseBody
    public String addUser( @RequestBody User user) {
        List<User> all = userService.getAll();
        for(int i = 0;i<all.size();i++){
            if(all.get(i).getIdcard().equals(user.getIdcard())){
                return "201";
            }
        }
        int d = userService.addUser(user);
        user.setStatus("00");
        return "200";
    }
    /**
     * 删除（驳回用户注册申请）
     */
    @RequestMapping( "/deleteUser")
    @ResponseBody
    public String deleteUser(String id) {
        int d = userService.deleteUser(id);
        return "UserList";
    }

    /**
     * 修改
     */
    @RequestMapping( "/updateUser")
    public String updateUser( User user) {
        int d = userService.updateUser(user);
        return "redirect:/findUser";
    }

    /**
     * 冻结账号
     */
    @RequestMapping( "/updateUserStatus")
    @ResponseBody
    public String updateAdminStatus(String id, String status) {
        try{
            User user = new User();
            user.setId(id);
            user.setStatus(status);
            int d = userService.updateUser(user);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 按照ID查询
     */
    @RequestMapping( "/findUserById")
    public String findUserById(String id,Model model,HttpServletRequest request) {
        User user= userService.findUserById(id);
        model.addAttribute("user",user);
        return "UserEdit";
    }


}
